#include "tree.h"

bool start( char *option )
{	
	if( strcmp( option, "-lp" ) == 0 || strcmp( option, "-l" ) == 0  )	{
	}
	else {
		fprintf( stderr, "Invalid option: %s\n", option );
		return false;
	}

	int now_line=0;
	while(true)
	{
		char buff[1024];
		if( fgets( buff, 1024, stdin ) == NULL )	break;
		now_line++;

		if( now_line % 1000 == 0 )	{
			fprintf( stderr, "\r%d sentences processed... ", now_line );
		}


		CTree *t = new CTree();
		t->Init();
		if( t->LoadTree( buff ) == false )	{
			fprintf( stderr, "load fail: %d\n", now_line );
			fprintf( stdout, "INVALID TREE\n" );
			delete t;
			continue;
		}

		int ls[NUM_MAX_NODE];
		int s;

		t->GetLexNodeList( ls, s, ROOT_ID );
		for( int i=0; i<s; i++ )	{
			CNode &n = t->node[ ls[i] ];
			CNode &p = t->node[ n.parent ];

			if( i > 0 )	{
				fprintf( stdout, " " );
			}

			if( strcmp( option, "-lp" ) == 0 )	{
				fprintf( stdout, "%s/%s", n.label.GetStr(), p.label.GetStr()  );
			}
			else if( strcmp( option, "-l" ) == 0 )	{
				fprintf( stdout, "%s", n.label.GetStr() );
			}
		}
		fprintf( stdout, "\n" );
		delete t;
	}
	fprintf( stderr, "\r%d sentences processed...\n", now_line );
	return true;
}


int main( int argc, char *argv[] )
{
	if( argc != 2 )	{
		fprintf( stderr, "usage: parse-conv [option] < STDIN > STDOUT\n" );
		fprintf( stderr, "   option: -lp -> Convert to Lex/Pos form\n" );
		fprintf( stderr, "           -l  -> Convert to Lex form\n" );
		return 1;
	}

	if( start( argv[1] ) == false )	{
		return 1;
	}

	return 0;
}


